//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __ELASTOS_EVENTSET_H__
#define __ELASTOS_EVENTSET_H__

class DzEventSet;
struct EventSetRefNode : public DLinkNode
{
    uint_t      uIndex;
    DzEventSet  *pEventSet;
};

class DzEventSet : public SyncObject
{
    friend class DzEvent;

public:
    DzEventSet(uint_t uNumberOfEvents, DzEvent **pEvents,
                    EventSetRefNode *pEventSetRefNodes);

    ~DzEventSet();

    void UninterruptibleWait(Int32 *pIndex, EventState *pState);
    void Wait(WaitResult *pResult, Int32 *pIndex, EventState *pState);

    void TryUninterruptibleWait(uint_t ticks, WaitResult *pResult,
                    Int32 *pIndex, EventState *pState);

    void TryWait(uint_t ticks, WaitResult *pResult, Int32 *pIndex,
                    EventState *pState);

private:
    void WaiterReadyToUninterruptibleSleep();
    void WaiterReadyToUninterruptibleSleep(DzTimer *pBindingTimer);

    void WaiterReadyToSleep(WaitResult *pResult);
    void WaiterReadyToSleep(DzTimer *pBindingTimer, WaitResult *pResult);

public:
    uint_t          m_uNumberOfEvents;
    DzEvent         **m_pEvents;
    EventSetRefNode *m_pEventSetRefNodes;
};

EXTERN void DzWaitForAnyEvent(Int32 uNumberOfEvents, DzEvent **pEvents,
                WaitResult *pResult, Int32 *pIndex, EventState *pState);

EXTERN void DzTryWaitForAnyEvent(uint_t uNumberOfEvents, DzEvent **pEvents,
                Millisecond32 ticks, WaitResult *pResult, Int32 *pIndex,
                EventState *pState);

EXTERN void DzWaitForAnyEventObj(uint_t uNumberOfEventObjs, IEvent **pEventObjs,
                WaitResult *pResult, Int32 *pIndex, EventState *pState);

EXTERN void DzTryWaitForAnyEventObj(uint_t uNumberOfEventObjs,
                IEvent **pEventObjs, Millisecond32 ticks, WaitResult *pResult,
                Int32 *pIndex, EventState *pState);

#endif //__ELASTOS_EVENTSET_H__
